#include <iostream>
#include <vector>
using namespace std;

int max_value_recur(vector<int> &m, vector<int> &p, int n, int k)
{
        if(n == 0){
                return 0;
        }

        int max_val = max_value_recur(m, p, n - 1, k);
        int id = n - 1;
        while(m[n] - m[id] <= k){
                id--;
        }
        max_val = max(max_val, max_value_recur(m, p, id, k) + p[n]);

        return max_val;
}


int max_value_recur(vector<int> &m, vector<int> &p, int n, int k, vector<int> &recd)
{
        if(n == 0){
                return 0;
        }

        if(recd[n] != -1){
                return recd[n];
        }

        int max_val = max_value_recur(m, p, n - 1, k, recd);
        int id = n - 1;
        while(id >= 0 && m[n] - m[id] <= k){
                id--;
        }
        max_val = max(max_val, max_value_recur(m, p, id, k, recd) + p[n]);

        return recd[n] = max_val;
}

int max_value_dp(vector<int> &m, vector<int> &p, int n, int k)
{
        vector<int> dp(n + 1, 0), pre(dp);
        dp[1] = p[1];
        for(int i = 2; i <= n; i++){
                for(int j = i - 1; j >= 0; j--){
                        if(m[i] - m[j] > k){
                                pre[i] = j;
                                break;
                        }
                }
        }

        for(int i = 2; i <= n; i++){
                dp[i] = max(dp[i - 1], dp[pre[i]] + p[i]);
        }

        return dp.back();
}

int main()
{
        int t;
        cin >> t;

        while(t--){
                int n, k;
                cin>>n>>k;
                vector<int> m(n + 1, 0), p(m);
                for(int i = 1; i <= n; i++){
                        cin>>m[i];
                }
                m[0] = -100;
                for(int i = 1; i <= n; i++){
                        cin>>p[i];
                }
                vector<int> recd(n + 1, -1);
                //cout<<max_value_recur(m, p, n, k, recd)<<endl;
                cout<<max_value_dp(m, p, n, k)<<endl;
        }

        return 0;
}